<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

use app\common\model\ForumBbs;
use app\common\model\ForumBbsComment;
use app\common\model\GamblerKingInvestmentUser;
use app\common\model\InvestmentCategory;

/**
 * 文章逻辑
 */
class Investment extends AdminBase
{



    /**
     * 获取文章列表
     */
    public function getInvestmentList($data = [], $field = 'a.*', $order = '')
    {

        $this->modelGamblerKingInvestment->alias('a');

        if (!empty($data['date']) && !empty($data['date1'])) {
            $where['a.create_time'] = ['between', [strtotime($data['date'] . ' 00:00:00'), strtotime($data['date1'] . ' 23:59:59')]];
        }
      // dd($data);
        if(!empty($data['search_data'])){
            $where['a.name']=['like','%'.$data['search_data'].'%'];
        }
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        $where['a.type'] = $data['type'];
        $count=$this->modelGamblerKingInvestment->alias('a')->where($where)->count();
        $list=$this->modelGamblerKingInvestment->alias('a')->field($field)->where($where)->paginate(50);

        return ['list'=>$list,'count'=>$count];
    }



    /**
     * 获取文章列表搜索条件
     */
    public function getWhere($data = [])
    {

        $where = [];

        !empty($data['search_data']) && $where['a.name|a.describe'] = ['like', '%'.$data['search_data'].'%'];

        return $where;
    }

    /**
     * 文章信息编辑
     */
    public function InvestmentEdit($data = [])
    {
//
        $validate_result = $this->validateInvestment->scene('edit')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateInvestment->getError()];
        }
        $url = url('InvestmentList');



        // empty($data['id']) && $data['member_id'] = MEMBER_ID;

//      $data['content'] = htmlspecialchars_decode($data['content']);
//        if(empty($data['id'])){
//            $count=  $this->modelGamblerKingInvestment->where(['type'=>$data['type'],'ranking'=>$data['ranking']])->count();
//            if($count>0){
//                return [RESULT_ERROR, '该排名已存在'];
//            }
//        }else{
//            $count=  $this->modelGamblerKingInvestment->where(['type'=>$data['type'],'ranking'=>$data['ranking']])->count();
//            if($count>1){
//                return [RESULT_ERROR, '该排名已存在'];
//            }elseif($count==1){
//                $info= $this->modelGamblerKingInvestment->getInfo(['ranking'=>$data['ranking']]);
//                if($info['id']!=$data['id']){
//                    return [RESULT_ERROR, '该排名已存在'];
//                }
//            }
//        }
        if(empty($data['id'])){
            $data['start_time_int']=strtotime(date('Y-m-d').' '.$data['start_time']);
        }else{
            $info= $this->modelGamblerKingInvestment->getInfo(['id'=>$data['id']]);

            $data['start_time_int']=strtotime(date('Y-m-d',$info['start_time_int']).' '.$data['start_time']);

        }
        $count=GamblerKingInvestmentUser::where(['state'=>1,'total_money'=>['gt',$data['superior_money']]])->count();
        if($count>0){
            return [RESULT_ERROR, '委托上限金额不能小于当前交易员已开始产品众筹总金额'];
        }
        $data['start_game_int']=$data['start_time_int']+$data['cycle']*86400;
        $data['start_game_end_int']=$data['start_game_int']+$data['receipt_minute']*60;
        $data['end_time_int']=$data['start_time_int']+($data['cycle']+$data['start_game_time'])*86400;
        $result = $this->modelGamblerKingInvestment->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';
        if(empty($data['name'])){
            $result && action_log($handle_text, '赌王产品' . $handle_text . '赌王投资');
        }else{
            $result && action_log($handle_text, '赌王产品' . $handle_text . '赌王心血公式，name：' . $data['name']);
        }


        return $result ? [RESULT_SUCCESS, '赌王产品操作成功', $url] : [RESULT_ERROR, $this->modelGamblerKingInvestment->getError()];
    }

    /**
     * 获取文章信息
     */
    public function getInvestmentInfo($where = [], $field = 'a.*,m.nickname,c.name as category_name')
    {

        $this->modelGamblerKingInvestment->alias('a');


        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];


        return $this->modelGamblerKingInvestment->getInfo($where, $field);
    }

 
    /**
     * 文章删除
     */
    public function InvestmentDel($where = [])
    {

        $result = $this->modelGamblerKingInvestment->deleteInfo($where);

        $result && action_log('删除', '赌王删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '赌王删除成功'] : [RESULT_ERROR, $this->modelGamblerKingInvestment->getError()];
    }

  


    /**
     * 文章分类编辑
     */
    public function InvestmentLevelEdit($data = [])
    {

        $validate_result = $this->validateInvestmentLevel->scene('edit')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateInvestmentLevel->getError()];
        }

        $url = url('InvestmentLevelList');

        $result = $this->modelInvestmentCategory->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '赌王分类' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelInvestmentCategory->getError()];
    }


    /**
     * 获取分类信息
     */
    public function getInvestmentLevelInfo($where = [], $field = true)
    {

        return $this->modelInvestmentCategory->getInfo($where, $field);
    }

    /**
     * 获取文章分类列表
     */
    public function getInvestmentLevelList($where = [], $field = true, $order = '', $paginate = 0)
    {

        return $this->modelInvestmentCategory->getList($where, $field, $order, $paginate);
    }

    /**
     * 文章分类删除
     */
    public function InvestmentLevelDel($where = [])
    {

        $result = $this->modelInvestmentCategory->deleteInfo($where);

        $result && action_log('删除', '赌王分类删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '赌王分类删除成功'] : [RESULT_ERROR, $this->modelInvestmentCategory->getError()];
    }
}
